package com.jiuyuejiu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jiuyuejiu.application.ConstValues;
import com.jiuyuejiu.base.BaseActivity;
import com.jiuyuejiu.func_video.VideoService;
import com.jiuyuejiu.utils.ShanHaiUtil;
import com.kylin.core.utils.dbtutil.FileUtil;
import com.kylin.core.utils.dbtutil.FunUtil;

import java.io.File;
import java.lang.ref.SoftReference;

import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

public class SplashActivity extends BaseActivity implements View.OnClickListener {

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存SOURC和RESULT
            .dontAnimate()// 移除所有的动画
            // .fitCenter()// 该api可能 铺不满整个ImageView控件
            .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
            .placeholder(R.drawable.shape_solid_cccccc)// 占位图
            //.transform(new CenterCrop(), new RoundedCorners(18))
            ;

    private ImageView img;


    private VideoService service;

    // 文件的MD5名称
    private String fileName;

    private MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<SplashActivity> fragmentRef;

        public MyHandler(SplashActivity fragment) {
            fragmentRef = new SoftReference<SplashActivity>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            SplashActivity fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();

            // 处理UI 变化
            switch (msg.what) {
                case ConstValues.WAIT0: // 获取首页封面的回调
                    fragment.getJsonListListSuc(bundle);
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        // 初始化视图
        initView();
        // 初始化数据
        initData();
    }

    // 初始化视图
    private void initView() {

        img = (ImageView) findViewById(R.id.main_img);

        Toast.makeText(SplashActivity.this, "正在优化路线", Toast.LENGTH_SHORT).show();

    }

    // 初始化数据
    private void initData() {

        handler = new MyHandler(SplashActivity.this);
        service = new VideoService(SplashActivity.this, handler);

        // 获取所有图片类型
        fileName = FunUtil.StringToMd5(ConstValues.LANDKING);
        FileUtil.deleteFile(new File(ShanHaiUtil.parseFilePath(fileName)));// 先删除
        String downurl = ShanHaiUtil.getHttpid() + ConstValues.LANDKING + "/" + ConstValues.TXTNAME;

        // 请求
        service.downCatVideoJsonList(downurl,// 请求url
                fileName, // 本地存储名称
                false, // 是否显示进度条 false不显示下载进度,true显示下载进度框
                ConstValues.WAIT0);// 用于当前Activity界面handle的对应回调接收 比如what1,what2

    }

    private void getJsonListListSuc(Bundle bundle) {
        String formjson = bundle.getString("formjson");
        String status = bundle.getString("status");
        if (ConstValues.SUCCESS.equals(status)) {

            FileUtil.deleteFile(new File(ShanHaiUtil.parseFilePath(fileName)));// 先删除

            Intent home = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(home);
            SplashActivity.this.finish();
        } else {
            Toast.makeText(SplashActivity.this, formjson, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /*case R.id.main_rl_back:// 头像
                if (ViewUtil.isDoubleClick(v.getId(), 800))
                    return;
                break;*/
        }
    }

    @Override
    public void onBackPressedSupport() {

        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {

            // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
            super.onBackPressedSupport();
            // finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            Toast.makeText(SplashActivity.this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置横向(和安卓4.x动画相同)
        return new DefaultHorizontalAnimator();
    }

}
